package com.tjetc.service.impl;

import com.tjetc.dao.CityMapper;
import com.tjetc.dao.SpotMapper;
import com.tjetc.dao.SpotRankTagMapper;
import com.tjetc.entity.City;
import com.tjetc.entity.Spot;
import com.tjetc.entity.SpotRankTag;
import com.tjetc.service.SpotRankTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SpotRankTagServiceImpl implements SpotRankTagService {

    @Autowired
    private SpotRankTagMapper spotRankTagMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private SpotMapper spotMapper;
    @Scheduled(cron = "0 0 0 * * ?")
    public void updateSpotTags() {
        // 清除旧标签
        spotRankTagMapper.deleteAllTags();

        List<SpotRankTag> newTags = new ArrayList<>();
        List<City> cities = cityMapper.selectALL();
        String[] dimensions = {"热度", "评分", "点赞"};
        LocalDateTime localDateTime = LocalDateTime.now();
        int limit = 10;
        for (City city : cities) {
            for (String dimension : dimensions) {
                List<Spot> topSpots = spotMapper.selectTopSpotsByCityAndDimension(Long.valueOf(city.getCode()), dimension, limit);
                for (int i = 0; i < topSpots.size(); i++) {
                    Spot spot = topSpots.get(i);
                     SpotRankTag spotRankTag = new SpotRankTag();
                     spotRankTag.setCityId(city.getId());
                     spotRankTag.setDimension(dimension);
                     spotRankTag.setTagName(city.getCityName() + dimension + "第" + (i + 1) + "名");
                     spotRankTag.setRank((long) (i+1));
                     spotRankTag.setLevel("市级");
                     spotRankTag.setProvinceId(5L);
                     spotRankTag.setSpotId(spot.getSpotId());
                     spotRankTag.setCreateTime(localDateTime);
                     spotRankTag.setUpdateTime(localDateTime);
                    newTags.add(spotRankTag);
                }
            }
        }

        // 省级：河北省 id = 1
        Long provinceId = 130000L;
        for (String dimension : dimensions) {
            List<Spot> topSpots = spotMapper.selectTopSpotsByProvinceAndDimension(provinceId, dimension, limit);
            for (int i = 0; i < topSpots.size(); i++) {
                Spot spot = topSpots.get(i);
                SpotRankTag spotRankTag = new SpotRankTag();
                spotRankTag.setDimension(dimension);
                spotRankTag.setTagName("河北" + dimension + "第" + (i + 1) + "名");
                spotRankTag.setRank((long) (i+1));
                spotRankTag.setLevel("省级");
                spotRankTag.setProvinceId(5L);
                spotRankTag.setSpotId(spot.getSpotId());
                spotRankTag.setCreateTime(localDateTime);
                spotRankTag.setUpdateTime(localDateTime);
                newTags.add(spotRankTag);
            }
        }

        // 插入标签
        spotRankTagMapper.insertBatch(newTags);

    }





    @Override
    public List<SpotRankTag> getTagsBySpotId(Long spotId) {

        return spotRankTagMapper.getTagsBySpotId(spotId);
    }


    @Override
    public List<SpotRankTag> getTagsByLevel(String level) {
        // 查询特定级别（市级、省级）的标签
        return spotRankTagMapper.getTagsByLevel(level);
    }




}
